//
//  MKMeMerchantViewController.swift
//  MOKOAnnounce
//
//  Created by 曹相召 on 2017/7/7.
//  Copyright © 2017年 cc.moko. All rights reserved.
//  我页面(商家和摄影师)

import UIKit

class MKMeMerchantViewController: MKBaseViewController,UITableViewDelegate,UITableViewDataSource {

    var listView:UITableView!
    var iconArr:Array<String>!
    var tipArr:Array<String>!
    var userInfoModel:MKUserInfoModel?
    var meAgentInfoModel:MKMeAgentInfoModel?
    var meAgentApplyModel:MKAgentApplyModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.red

        self.iconArr = ["newMeIcon_myZhaoMu","newMeIcon_realNameRenZheng","newMe_official_ broker","newMeIcon_mokoShow","newMeIcon_ziJinAccont","newMe_visual_marketing","newMeIcon_mokoTongJi","newMeIcon_lianXiMoko",""]
        self.tipArr = ["我的招募","专业认证","官方经纪","美空展示","工作退费","视觉营销","通告统计","联系美空",""]
        self.makeUI()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.initData()
    }
    func initData() -> Void {
    
        let url = "me/new/userid/".appending(String(MKUser.shareUser.userid))
        MKNetWorkTool.getRequest(url: url, params: nil, success: { (response, responseObject) in
            let model = MKUserInfoResponse.deserialize(from: responseObject)
            if (model?.resultcode?.isEqual("ok"))!{
                let userInfoModel = model?.info
                print(userInfoModel?.area!)
                print(userInfoModel?.userid)
                print(userInfoModel?.leavel)
                self.userInfoModel = userInfoModel
                self.listView.reloadData()
            }else{
                // 成功的其他情况的展示
            }
        }) { (response, error) in
            
        }
        
        //获取商家的购买历史数量,会员余额,收藏历史数据
        let meAgentInfoUrl = "me/agent/info"
        MKNetWorkTool.getRequest(url: meAgentInfoUrl, params: nil, success: { (response, responseObject) in
            let model = MKMeAgentInfoResponse.deserialize(from: responseObject)
            if (model?.resultcode?.isEqual("ok"))!{
                    if let meAgentInfoModel = model?.info{
                    print(meAgentInfoModel.agentKeepCount!)
                    print(meAgentInfoModel.agentAccountCount!)
                    print(meAgentInfoModel.agentBuyCount!)
                    self.meAgentInfoModel = meAgentInfoModel
                    self.listView.reloadData()
                }
               
            }else{
                // 成功的其他情况的展示
            }
        }) { (response, error) in
            
        }
 
        let agentapplyNumberUrl = "agentapply/number"
        MKNetWorkTool.getRequest(url: agentapplyNumberUrl, params: nil, success: { (response, responseObject) in
            let model = MKAgentApplyResponse.deserialize(from: responseObject)
            if (model?.resultcode?.isEqual("ok"))!{
                if let meAgentApplyModel = model?.info{
                    print(meAgentApplyModel.aragentCount1)
                    print(meAgentApplyModel.aragentCount2)
                    print(meAgentApplyModel.aragentCount3)
                    print(meAgentApplyModel.aragentCount4)
                    self.meAgentApplyModel = meAgentApplyModel
                    self.listView.reloadData()
                }
            }else{
                // 成功的其他情况的展示
            }
        }) { (response, error) in
            
        }

        
    }
    func makeUI() -> Void {
        
        self.titleBar =
            MKTitleBar().titleBar(title: "我的", leftStyle: .None, rightStyle: .Setting, barStyle: .Default)
        self.titleBar?.delegate = self
        self.view.addSubview(self.titleBar!)
        
        self.listView = UITableView.init(frame: CGRect.init(x: 0, y: 64, width: ScreenWidth, height: ScreenHeight - 64), style: .grouped)
        self.listView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0)
        self.listView.separatorStyle = .none
        self.listView.showsVerticalScrollIndicator = false
        self.listView.rowHeight = 50;
        self.listView.delegate = self as UITableViewDelegate
        self.listView.dataSource = self as UITableViewDataSource
        self.listView .register(MKMeHeaderCell.classForCoder(), forCellReuseIdentifier: "MKMeHeaderCell")
        self.listView .register(MKMyViewMerchantThreeTabCell.classForCoder(), forCellReuseIdentifier: "MKMyViewMerchantThreeTabCell")
        self.listView .register(MKMyViewOrdersCell.classForCoder(), forCellReuseIdentifier: "MKMyViewOrdersCell")
        self.listView .register(MKMyCollectionViewCell.classForCoder(), forCellReuseIdentifier: "MKMyCollectionViewCell")
        self.view.addSubview(self.listView)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if(section == 0)
        {
            return 2;
        }
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if(indexPath.section == 0 && indexPath.row == 0){
            let cell = tableView.dequeueReusableCell(withIdentifier: "MKMeHeaderCell", for: indexPath) as! MKMeHeaderCell
            cell.userInfoModel = self.userInfoModel
            return cell
        }
        else if(indexPath.section == 0 && indexPath.row == 1){
            let cell = tableView.dequeueReusableCell(withIdentifier: "MKMyViewMerchantThreeTabCell", for: indexPath) as! MKMyViewMerchantThreeTabCell
            cell.meAgentInfoModel = self.meAgentInfoModel
            return cell
        }
        else if(indexPath.section == 1){
            let cell = tableView.dequeueReusableCell(withIdentifier: "MKMyViewOrdersCell", for: indexPath) as! MKMyViewOrdersCell
            var badge:Array<Int> = [0,0,0,0,0]
            if let meAgentApplyModel = self.meAgentApplyModel{
               badge = [meAgentApplyModel.aragentCount1,meAgentApplyModel.aragentCount2,meAgentApplyModel.aragentCount3,meAgentApplyModel.aragentCount4,0]
            }
            cell.config(badges: badge, cellType: .Business)
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "MKMyCollectionViewCell", for: indexPath) as! MKMyCollectionViewCell
                cell.config(tipArr: self.tipArr, iconArr: self.iconArr)
            return cell
        }
        
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if(indexPath.section == 0 && indexPath.row == 0){
            return 164.5
        }
        else if(indexPath.section == 0 && indexPath.row == 1)
        {
          return 60
        }else if(indexPath.section == 1){
            return 83
        }
        return 300
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if(section == 1 || section == 2){
            return 8
        }
        return 0.1;
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1;
    }

    func titleBarLeftClick() {
        print("titleBarLeftClick")
    }
    
    func titleBarRightClick() {
        let settingVC:MKSettingViewController = MKSettingViewController()
        self.navigationController?.pushViewController(settingVC, animated: true)
        print("titleBarRightClick")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
